1   /*
2    * Copyright (C) 2008 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.util.concurrent;
18  
19  import com.google.common.base.Function;
20  
21  import java.lang.reflect.UndeclaredThrowableException;
22  
23  /**
24   * Unit tests for {@link Futures#transform(ListenableFuture, Function)}.
25   *
26   * @author Nishant Thakkar
27   */
28  public class FuturesTransformTest
29      extends AbstractChainedListenableFutureTest<String> {
30    private static final String RESULT_DATA = "SUCCESS";
31  
32    @Override protected ListenableFuture<String> buildChainingFuture(
33        ListenableFuture<Integer> inputFuture) {
34      return Futures.transform(inputFuture,
35          new ComposeFunction());
36    }
37  
38    @Override protected String getSuccessfulResult() {
39      return RESULT_DATA;
40    }
41  
42    private class ComposeFunction
43        implements Function<Integer, String> {
44      @Override
45      public String apply(Integer input) {
46        if (input.intValue() == VALID_INPUT_DATA) {
47          return RESULT_DATA;
48        } else {
49          throw new UndeclaredThrowableException(EXCEPTION);
50        }
51      }
52    }
53  
54    public void testFutureGetThrowsFunctionException() throws Exception {
55      inputFuture.set(EXCEPTION_DATA);
56      listener.assertException(EXCEPTION);
57    }
58  }